package com.wizinno.android.urineflow.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.wizinno.android.urineflow.R;
import com.wizinno.android.urineflow.constant.CommonControl;
import com.wizinno.android.urineflow.constant.FragmentNoConstant;
import com.wizinno.android.urineflow.dialog.CustomDialog;
import com.wizinno.android.urineflow.serialport.SerialPortUtil;

public class WeightFragment extends BaseFragment {

    private Button btn_weight_read;
    private Button btn_adc_read;


    private Button btn_check_0_write;
    private Button btn_check_500_write;

    private EditText et_weight;
    private EditText et_adc;
    private EditText et_check_0;
    private EditText et_check_500;

    public WeightFragment() {
        setNo(FragmentNoConstant.SETTING_WEIGHT);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btn_weight_read = view.findViewById(R.id.btn_weight_read);
        btn_adc_read = view.findViewById(R.id.btn_adc_read);
        btn_check_0_write = view.findViewById(R.id.btn_check_0_write);
        btn_check_500_write = view.findViewById(R.id.btn_check_500_write);

        et_weight = view.findViewById(R.id.et_weight);
        et_adc = view.findViewById(R.id.et_adc);
        et_check_0 = view.findViewById(R.id.et_check_0);
        et_check_500 = view.findViewById(R.id.et_check_500);

        WeightClickListener weightClickListener = new WeightClickListener();
        btn_weight_read.setOnClickListener(weightClickListener);
        btn_adc_read.setOnClickListener(weightClickListener);
        btn_check_0_write.setOnClickListener(weightClickListener);
        btn_check_500_write.setOnClickListener(weightClickListener);
    }

    @Override
    protected void initData() {
        setWriteBtn();
        SerialPortUtil.readAdc();
        try {
            Thread.sleep(200);
            startWeight();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void setWriteBtn() {
        if (CommonControl.INSTANCE.isAdmin) {
            btn_check_0_write.setEnabled(true);
            btn_check_500_write.setEnabled(true);
            et_adc.setFocusable(true);
        } else {
            btn_check_0_write.setEnabled(false);
            btn_check_500_write.setEnabled(false);
            et_adc.setFocusable(false);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_weight, container, false);
    }

    public void setWeight(byte[] receiveBytes) {
        float weight = SerialPortUtil.transWeight(receiveBytes);
        et_weight.setText(String.valueOf(weight));
    }

    public void setAdc(byte[] receiveBytes) {
        Integer adc = SerialPortUtil.transAdc(receiveBytes);
        et_adc.setText(String.valueOf(adc));
    }

    public void setCheck0(byte[] receiveBytes) {
        Integer adc = SerialPortUtil.transAdc(receiveBytes);
        et_check_0.setText(String.valueOf(adc));
    }

    public void setCheck500(byte[] receiveBytes) {
        Integer adc = SerialPortUtil.transAdc(receiveBytes);
        et_check_500.setText(String.valueOf(adc));
    }

    /**
     * 切换采集状态
     */
    private void switchWeight() {
        if (CommonControl.INSTANCE.isWight) {
            stopWeight();
        } else {
            startWeight();
        }
    }

    private void startWeight() {
        SerialPortUtil.startWeight();
        btn_weight_read.setText(R.string.end_weight);
    }

    private void stopWeight() {
        SerialPortUtil.stopWeight();
        btn_weight_read.setText(R.string.start_weight);
    }

    private void showCheck500Dialog() {
        CustomDialog customDialog = new CustomDialog(CommonControl.INSTANCE.settingBaseActivity);
        customDialog.setMessage("确定将ADC读数:" + et_adc.getText() + "写入500g校准值？");
        customDialog.setPositiveButton((dialog, which) -> {
            SerialPortUtil.writeCheck500(Integer.valueOf(et_adc.getText().toString()));
            dialog.dismiss();
        });
        customDialog.show();
    }

    private void showCheck0Dialog() {
        CustomDialog customDialog = new CustomDialog(CommonControl.INSTANCE.settingBaseActivity);
        customDialog.setMessage("确定将ADC读数:" + et_adc.getText() + "写入零点校准值？");
        customDialog.setPositiveButton((dialog, which) -> {
            SerialPortUtil.writeCheck0(Integer.valueOf(et_adc.getText().toString()));
            dialog.dismiss();
        });
        customDialog.show();
    }

    class WeightClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            switch (viewId) {
                case R.id.btn_weight_read:
                    switchWeight();
                    break;
                case R.id.btn_adc_read:
                    stopWeight();
                    SerialPortUtil.readAdc();
                    break;
                case R.id.btn_check_0_write:
                    stopWeight();
                    showCheck0Dialog();
                    break;
                case R.id.btn_check_500_write:
                    stopWeight();
                    showCheck500Dialog();
                    break;
            }
        }
    }
}